﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class TestCheckout
    {
        private ChromeDriver driver;
        string path;
        private string url = "localhost:8080";
        private Action action;
        [TestInitialize]
        public void Setup()
        {
            string projectname = "seleniumtesting111";
            string path = Directory.GetCurrentDirectory().Substring(0, Directory.GetCurrentDirectory().LastIndexOf(@"seleniumtesting111") + projectname.Length);
            ChromeOptions incognito = new ChromeOptions();
            incognito.AddArgument("--incognito");
            driver = new ChromeDriver(path, incognito);
            //driver.Navigate().GoToUrl(url);
        }

        [TestCleanup]
        public void CleanUp()
        {
            driver.Quit();
        }

        [TestMethod]
        [Description("Adds and item to basket and checks if present")]
        public void Test_add_to_basket()
        {
            driver.Navigate().GoToUrl(url + "/projects/view/83/");
            driver.FindElementById("donate-box").FindElement(By.Name("submit")).Click();
            driver.Navigate().GoToUrl(url + "/cart");
            var value = driver.FindElementByTagName("h2").Text;
            Assert.AreEqual(value, "Donation");
        }

        
        [TestMethod]
        [Description("Adds and item to basket, logs in and fills out the information, goes to epay, fills out bank cresidentiels" +
                     "and receive receipt with right order")]
        public void Test_go_to_checkout_and_receive_receipt()
        {
            //adds items to basket and goes to checkout/information to log in
            driver.Navigate().GoToUrl(url + "/projects/view/83/");
            driver.FindElementById("donate-box").FindElement(By.Name("submit")).Click();
            driver.Navigate().GoToUrl(url + "/checkout/information");
            var section = driver.FindElementByClassName("notloggedin ");
            section.FindElement(By.Id("login")).SendKeys("seleniumtest@showyoucare.dk");
            section.FindElement(By.Id("password")).SendKeys("showyoucare");
            driver.FindElementByXPath("/html/body/div[1]/section/div[1]/form/div[5]/input").Click();

            //Fills out the information
            driver.FindElementByClassName("btn").Click();
            driver.SwitchTo().Frame("epay_frame");
            var elem = driver.FindElementByClassName("epay_payment_logos");
            elem.Click();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));
            driver.FindElementById("ctl00_MainContent_WindowUC1_ctl00_ctl00_Logo1").Click();
            driver.FindElementById("ctl00_MainContent_WindowUC1_ctl00_ctl00_btnSubmitForm").Click();
            //Receipt
            Assert.IsTrue(driver.FindElementByClassName("product-info").Text.Contains("Støt piger og unge kvinders rettigheder i Benin"));
        }


        [TestMethod]
        [Description("Checks if the donation value increments with a purchase order")]
        public void Test_donation_value_is_incremented()
        {
            //adds items to basket and goes to checkout/information to log in
            driver.Navigate().GoToUrl(url + "/projects/view/83/");
            double start = double.Parse(driver.FindElementById("data").FindElement(By.TagName("span")).Text);
            start += 10;
            driver.FindElementById("donate-box").FindElement(By.Name("submit")).Click();
            driver.Navigate().GoToUrl(url + "/checkout/information");
            var section = driver.FindElementByClassName("notloggedin ");
            section.FindElement(By.Id("login")).SendKeys("seleniumtest@showyoucare.dk");
            section.FindElement(By.Id("password")).SendKeys("showyoucare");
            driver.FindElementByXPath("/html/body/div[1]/section/div[1]/form/div[5]/input").Click();

            //Fills out the information
            driver.FindElementByClassName("btn").Click();
            driver.SwitchTo().Frame("epay_frame");
            var elem = driver.FindElementByClassName("epay_payment_logos");
            elem.Click();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));
            driver.FindElementById("ctl00_MainContent_WindowUC1_ctl00_ctl00_Logo1").Click();
            driver.FindElementById("ctl00_MainContent_WindowUC1_ctl00_ctl00_btnSubmitForm").Click();

            //go to project list view
            driver.Navigate().GoToUrl(url + "/projects/view/83/");
            double end = Double.Parse(driver.FindElementById("data").FindElement(By.TagName("span")).Text);
            Assert.AreEqual(end, start);
        }

        [TestMethod]
        [Description("See if CRON job doesnt return any error statuses")]
        public void Test_if_cron_monthly_subscriptions_is_success()
        {
            driver.Navigate().GoToUrl(url + "/cron/monthly_subscriptions");
            var value = driver.FindElementByCssSelector("*");
            Assert.IsTrue(value.Text.Contains("All Done. Number of subscriptions"));    
        }

        [TestMethod]
        [Description("See if callback for ePay returns an error if order doesnt exists")]
        public void Test_payment_callback_returns_error_on_no_order()
        {
            driver.Navigate().GoToUrl(url + "/checkout/payment_callback?txnid=13232540&orderid=415&amount=6500&currency=208&date=20121108&time=1316&txnfee=99&subscriptionid=592012&paymenttype=4&cardno=555555XXXXXX5000&hash=1b672d3564c217ca4014546b76f0944d");
            var value = driver.FindElementByCssSelector("*");
            Assert.IsTrue(value.Text.Contains("No orderId found"));
        }

        /**
         *  Test case to test when a payment callback is successfull.
         *
         *  This can not be an automatically test, because the request alteres the order in the database.
         *  Edit orderid to an test order in the PurchaseOrder table which has an 
         *  unpayed ePay payment in the Payment table.
         *  
         *  WebTestCase does a seperate socket connection, and 
         *  actually returns the correct fetched data, from a new thread. 
         *  This means that the database can't be mocked, and 
         *  whenever a case is chaning the data, it is too late.
         */
        [TestMethod]
        [Description("See if callback for ePay returns an error if order doesnt exists")]
        public void test_payment_callback_returns_true_on_order_found()
        {
            driver.Navigate().GoToUrl(url + "/checkout/payment_callback?txnid=13232540&orderid=415&amount=6500&currency=208&date=20121108&time=1316&txnfee=99&subscriptionid=592012&paymenttype=4&cardno=555555XXXXXX5000&hash=1b672d3564c217ca4014546b76f0944d");
            var value = driver.FindElementByCssSelector("*");
            Assert.IsTrue(value.Text.Contains("All Done"));
        }
    }
}
